import { Component } from 'vue';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { shadowBeforeEach } from '@x-tech/fe-starter-pc-shadow/src/router';
import store from '@/store';
import { ElMessage } from 'element-plus';

// 路由
export interface RouterMeta {
    icon: string;
    roles: string[];
    title: string;
    hidden: boolean;
    alwaysShow: boolean;
    keepAlive: boolean;
    rank: number;
    componentName: string;
}

export interface RouteRaw {
    path: string;
    name: string;
    children: RouteRaw[];
    component: () => Component;
    meta: RouterMeta;
}

export const constRoutes = [
    {
        path: '/',
        name: 'Index',
        component: () => import('@/views/Layout/index.vue'),
        // redirect: '/login',
        meta: {
            icon: 'icon',
            roles: [],
            hidden: true,
        },
    },

    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/newLogin/index.vue'),
        meta: {
            title: '登录',
            hidden: true,
        },
    },
    {
        path: '/wxLogin',
        name: 'wxLogin',
        component: () => import('@/views/newLogin/components/WxLogin.vue'),
        meta: {
            title: '微信登录',
            hidden: true,
        },
    },
];

export const asyncRoutes = [
    {
        path: '/resource',
        component: () => import('@/views/Layout/index.vue'),
        meta: {
            icon: 'icon-home_resource',
            roles: ['resource'],
            title: '资源',
            hidden: false,
            alwaysShow: false,
            isKeepAlive: true,
        },
        children: [
            {
                path: '/resource/houseManage/index',
                component: () => import('@/views/resource/houseManage/index.vue'),
                redirect: '/resource/houseManage/auditPage',
                meta: {
                    icon: 'setting',
                    roles: ['resource_houseManage_index'],
                    title: '楼盘管理',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/resource',
                },
                children: [
                    {
                        path: '/resource/houseManage/auditPage',
                        component: () => import('@/views/resource/houseManage/AuditPage.vue'),
                        meta: {
                            icon: 'setting',
                            roles: [],
                            hidden: true,
                            alwaysShow: false,
                            isKeepAlive: true,
                        },
                    },
                ],
            },
            {
                path: '/resource/pointManage/index',
                component: () => import('@/views/resource/pointManage/Index.vue'),
                meta: {
                    icon: 'setting',
                    roles: ['resource_pointManage_index'],
                    title: '点位管理',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/resource',
                },
            },
        ],
    },
    {
        path: '/delivery',
        name: 'Delivery',
        component: () => import('@/views/Layout/index.vue'),
        meta: {
            icon: 'icon-home_data',
            roles: ['delivery'],
            title: '投放',
            hidden: false,
            alwaysShow: false,
            isKeepAlive: true,
        },
        children: [
            {
                path: '/customerManage',
                name: 'CustomerManage',
                component: () => import('@/views/delivery/cusManage/index.vue'),
                meta: {
                    icon: 'house',
                    roles: ['customerManage'],
                    title: '签约管理',
                    hidden: false,
                    parentCode: '/delivery',
                },
            },
            {
                path: '/creativeManage',
                name: 'CreativeManage',
                component: () => import('@/views/delivery/creativeManage/index.vue'),
                meta: {
                    icon: 'house',
                    roles: ['creativeManage'],
                    title: '创意管理',
                    hidden: false,
                    parentCode: '/delivery',
                },

                children: [
                    {
                        path: '',
                        name: 'creativeManage',
                        component: () => import('@/views/delivery/creativeManage/components/homeManage.vue'),
                        meta: {
                            icon: 'house',
                            roles: ['creativeManage'],
                            title: '',
                            hidden: false,
                            parentCode: '/delivery',
                        },
                    },
                    {
                        path: '/addManage',
                        name: 'addManage',
                        component: () => import('@/views/delivery/creativeManage/components/addManage.vue'),
                        meta: {
                            icon: 'house',
                            roles: ['creativeManage'],
                            title: '新增创意',
                            hidden: false,
                            parentCode: '/creativeManage',
                        },
                    },
                    {
                        path: '/editManage/:id',
                        name: 'editManage',
                        component: () => import('@/views/delivery/creativeManage/components/addManage.vue'),
                        meta: {
                            icon: 'house',
                            roles: ['creativeManage'],
                            title: '编辑创意',
                            hidden: false,
                            parentCode: '/creativeManage',
                        },
                    },
                ],
            },

            {
                path: '/delivery/program',
                name: 'Program',
                component: () => import('@/views/delivery/program/index.vue'),

                meta: {
                    icon: 'house',
                    roles: ['delivery_program'],
                    title: '方案管理',
                    hidden: false,
                    parentCode: '/delivery',
                },
            },

            {
                path: '/delivery/program/create',
                name: 'CreatProgram',
                component: () => import('@/views/delivery/program/createProgram.vue'),

                meta: {
                    title: '新建方案',
                    hidden: true,
                    parentCode: '/delivery',
                    // closeDefaultBgColor: true, // 关闭PUMA-MAIN组件容器默认样式
                },
            },
            {
                path: '/delivery/program/viewPhoto',
                name: 'viewPhoto',
                component: () => import('@/views/delivery/program/viewPhoto.vue'),

                meta: {
                    title: '照片查看',
                    hidden: true,
                    parentCode: '/delivery',
                    // closeDefaultBgColor: true, // 关闭PUMA-MAIN组件容器默认样式
                },
            },
            {
                path: '/delivery/makeCarryBroadcast',
                name: 'CarryBroadcast',
                component: () => import('@/views/delivery/carryBroadcast/makeCarryBroadcast.vue'),
                meta: {
                    title: '制作报告-检查照片',
                    hidden: true,
                    parentCode: '/delivery',
                },
            },
            {
                path: '/delivery/viewReport',
                name: 'ViewReport',
                component: () => import('@/views/delivery/carryBroadcast/viewReport.vue'),
                meta: {
                    title: '查看报告',
                    hidden: true,
                    parentCode: '/delivery',
                    // closeDefaultBgColor: true, // 关闭PUMA-MAIN组件容器默认样式
                },
            },
            {
                path: '/delivery/checkScreen',
                name: 'CheckScreen',
                component: () => import('@/views/delivery/carryBroadcast/checkScreen.vue'),
                meta: {
                    title: '制作报告-检查截帧图',
                    hidden: true,
                    parentCode: '/delivery',
                    // closeDefaultBgColor: true, // 关闭PUMA-MAIN组件容器默认样式
                },
            },
        ],
    },
    {
        path: '/publication',
        component: () => import('@/views/Layout/index.vue'),
        meta: {
            icon: 'icon-shangkan',
            roles: ['publication'],
            title: '上刊',
            hidden: false,
            alwaysShow: false,
            isKeepAlive: true,
        },
        children: [
            {
                path: '/advertisingPush',
                name: 'AdvertisingPush',
                component: () => import('@/views/publication/advertisingPush/index.vue'),

                meta: {
                    icon: 'house',
                    roles: ['advertisingPush'],
                    title: '广告推送',
                    hidden: false,
                    parentCode: '/publication',
                },
            },
            {
                path: '/taskProgress',
                name: 'TaskProgress',
                component: () => import('@/views/publication/taskProgress/index.vue'),

                meta: {
                    icon: 'house',
                    roles: ['taskProgress'],
                    title: '任务进度',
                    hidden: false,
                    parentCode: '/publication',
                },
            },
            {
                path: '/publication/route/index',
                component: () => import('@/views/publication/route/index.vue'),
                meta: {
                    icon: 'setting',
                    roles: ['route'],
                    title: '路线管理',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/publication',
                },
            },
        ],
    },
    {
        path: '/finance',
        component: () => import('@/views/Layout/index.vue'),
        meta: {
            icon: 'icon-caiwu',
            roles: ['finance'],
            title: '财务',
            hidden: false,
            alwaysShow: false,
            isKeepAlive: true,
        },
        children: [
            {
                path: '/finance/dataReport',
                component: () => import('@/views/finance/dataReport.vue'),
                meta: {
                    icon: 'setting',
                    roles: ['dataReport'],
                    title: '数据报表',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/finance',
                },
            },
        ],
    },
    {
        path: '/account',
        component: () => import('@/views/Layout/index.vue'),
        meta: {
            icon: 'icon-shezhi',
            roles: ['account'],
            title: '设置',
            hidden: false,
            alwaysShow: false,
            isKeepAlive: true,
        },
        children: [
            {
                path: '/account/auditManage/index',
                component: () => import('@/views/account/auditManage/Index.vue'),
                meta: {
                    icon: 'setting',
                    roles: ['account_auditManage_index'],
                    title: '审批管理',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/account',
                },
            },
            {
                path: '/account/targetManage/index',
                component: () => import('@/views/account/targetManage/Index.vue'),
                redirect: '/account/targetManage/targetPage',
                meta: {
                    icon: 'setting',
                    roles: ['account_targetManage_index'],
                    title: '目标管理',
                    hidden: false,
                    alwaysShow: false,
                    isKeepAlive: true,
                    parentCode: '/account',
                },
                children: [
                    {
                        path: '/account/targetManage/targetPage',
                        component: () => import('@/views/account/targetManage/TargetPage.vue'),
                        meta: {
                            icon: 'setting',
                            roles: [],
                            hidden: true,
                            alwaysShow: false,
                            isKeepAlive: true,
                        },
                    },
                ],
            },
        ],
    },
];

const routes = [...constRoutes, ...asyncRoutes];
const pumaRoutes = store.state.puma.routes;
store.dispatch('puma/setRoutes', pumaRoutes);

const router = createRouter({
    history: createWebHashHistory(),
    routes: routes as Array<RouteRecordRaw>,
});

router.beforeEach(shadowBeforeEach);
router.beforeEach((to, from, next) => {
    const token = store.state.puma.token;
    const isVisitLogin = ['/login', '/wxLogin'].includes(to.path);
    if (!token) {
        if (isVisitLogin) {
            next();
            return;
        }
        next('/login');
        return;
    }
    if (to.path === '/') {
        const pumaRoutes = store.state.puma.routes;
        if (!pumaRoutes.length) {
            ElMessage.error('无访问权限，请联系管理员');
            next('/login');
            return;
        }
        const replacePath = pumaRoutes[0].children[0].path;
        next(replacePath);
        return;
    }
    if (isVisitLogin) {
        next(from.path);
        return;
    }
    next();
});

export default router;
